function getNowMonth() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var d = date.getDate()
    month = month > 9 ? month : "0" + month;
    var nowMonth = year + "-" + month + '-' + d;
    return nowMonth;
}
/**获取上一个月 */
function getPreMonth() {
    var arr = getNowMonth().split("-");
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var d = arr[2]; //获取当前日期天
    var year2 = year;
    var month2 = parseInt(month) - 1;
    if (month2 == 0) {
        //1月的上一月是前一年的12月
        year2 = parseInt(year2) - 1;
        month2 = 12;
    }

    if (month2 < 10) {
        //10月之前都需要补0
        month2 = "0" + month2;
    }
    var preMonth = year2 + "-" + month2 + '-' + d;
    return preMonth;
}
//时间控件
layui.use('laydate', function() {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#test-range',
        range: ['#startDate', '#endDate'],
        max: getNowMonth(),
        value: getPreMonth() + ' - ' + getNowMonth(),

        done: function(value, date, endDate) {
            if (date.month < 10) {
                //10月之前都需要补0
                date.month = "0" + date.month;
            }
            if (endDate.month < 10) {
                //10月之前都需要补0
                endDate.month = "0" + endDate.month;
            }
            var num1 = date.year + '-' + date.month + '-' + date.date;
            var num2 = endDate.year + '-' + endDate.month + '-' + endDate.date;
            getTime(num1, num2);
        }
    });

});

function getTime(num1, num2) {
    $.ajax({
                url: `${Url}/Wechat/Api/ExamList?IdentityNo=${theRequest.IdentityNo}&BeginTime=${num1}&EndTime=${num2}`,
                type: 'get',
                datatype: 'json',
                headers: {
                    'Authorization': Authorization
                },
                async: false,
                success: function(res) {
                        if (res.data.ExamModels.length > 0) {

                            var IdentityNo = res.data.IdentityNo.substring(0, 3) + '****' + res.data.IdentityNo.substring(res.data.IdentityNo.length - 4, res.data.IdentityNo.length);
                            let teplhtml = `
                            ${res.data.ExamModels.map(item=>{
                              return ` <li><header>体检人:<span>${res.data.FullName}</span> 身份证号:<span>${IdentityNo}</span></header>
                              <h2>健康体检报告</h2>
                              <p>体检日期: <span>${item.CheckDate}</span></p>
                             <button id="${item.ExamId}">查看报告</button></li>`
                          }).join('')
                      }
                           `
                            $('ul').html(teplhtml);

                            for (const key in res.data.ExamModels) {
                                $('ul li button').click(function() {
                                    const id=$(this).attr("id")
                                    window.location.href=`historypage?ExamId=${id}&Authorization=${Authorization}`
                                })
                            }
                           
                        }else{
                            $('ul li').css('display','none')
                        }
                        
                       
        }
    })
}
getTime(getPreMonth(), getNowMonth());
//获取历史体检数据